package com.example.pest.controller.manage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pest.bean.PTResult;
import com.example.pest.pojo.XxBanner;
import com.example.pest.pojo.XxNotice;
import com.example.pest.pojo.XxRoom;
import com.example.pest.pojo.XxUser;
import com.example.pest.service.impl.*;
import com.example.pest.util.JwtUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@AllArgsConstructor
@RequestMapping("manage")
public class ManageUserController {

    private final XxUserServiceImpl userService;
    private final XxBannerServiceImpl bannerService;
    private final XxNoticeServiceImpl noticeService;
    private final XxLiuyanServiceImpl liuyanService;
    private final XxRoomServiceImpl roomService;

    @GetMapping("/getAllUser")
    public PTResult<Page<List<XxUser>>> findUserByAll(@RequestParam(value = "role",required = false) String role, @RequestParam(value = "phone",required = false) String phone,@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "10") int limit) {
        int index = (page - 1) * limit;
        List<XxUser> alluser= userService.lambdaQuery().eq(role != null ,XxUser::getRole,role).like(phone != null ,XxUser::getPhone,phone).orderByDesc(XxUser::getId).last(String.format(" limit %d,%d",index,limit)).list();
        long total = userService.lambdaQuery().eq(role != null ,XxUser::getRole,role).eq(phone != null ,XxUser::getPhone,phone).count();
        Page result = new Page(page,limit,total);
        result.setRecords(alluser);
        return PTResult.ok(result);
    }

    /*
    删除用户信息
    * */
    @PostMapping("deleteUser")
    public PTResult deleteUser( @RequestBody  XxUser user) {
        QueryWrapper<XxUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",user.getId());
        userService.remove(queryWrapper);
        return PTResult.ok();
    }

    @PostMapping("/updateUserInfo")
    public PTResult updateUserInfo( @RequestBody XxUser user) {
        LambdaUpdateWrapper<XxUser> updateWrapper = new LambdaUpdateWrapper();
        updateWrapper.eq(XxUser::getId,user.getId())
                .set(XxUser::getPhone,user.getPhone())
                .set(XxUser::getPassword,user.getPassword())
                .set(XxUser::getSex,user.getSex())
                .set(XxUser::getUsername,user.getUsername());
        if (JwtUtil.isManage()){
            updateWrapper.set(XxUser::getRole,user.getRole());
        }
        userService.update(updateWrapper);
        return PTResult.ok();
    }

    @GetMapping("/getRoomList")
    public PTResult<Page<List<XxRoom>>> getAllRoom(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "10") int limit) {
        int index = (page - 1) * limit;
        List<XxRoom> list = roomService.lambdaQuery().like(name != null ,XxRoom::getName,name).orderByDesc(XxRoom::getId).last(String.format(" limit %d,%d",index,limit)).list();
        long total = roomService.lambdaQuery().like(name != null ,XxRoom::getName,name).count();
        Page result = new Page(page,limit,total);
        result.setRecords(list);
        return PTResult.ok(result);
    }

    @PostMapping("/addOrEditRoom")
    public PTResult addOrEditRoom(@RequestBody XxRoom room) {
        roomService.saveOrUpdate(room);
        return PTResult.ok();
    }

    @GetMapping("/getBannerList")
    public PTResult<Page<List<XxBanner>>> getBannerList( @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "10") int limit) {
        int index = (page - 1) * limit;
        List<XxBanner> list = bannerService.lambdaQuery().orderByDesc(XxBanner::getSort).orderByDesc(XxBanner::getId).last(String.format(" limit %d,%d",index,limit)).list();
        long total = bannerService.lambdaQuery().count();
        Page result = new Page(page,limit,total);
        result.setRecords(list);
        return PTResult.ok(result);
    }

    @PostMapping("/addOrEditBanner")
    public PTResult addOrEditBanner(@RequestBody XxBanner room) {
        bannerService.saveOrUpdate(room);
        return PTResult.ok();
    }

    @PostMapping("/deleteBanner")
    public PTResult deleteBanner(@RequestBody XxBanner room) {
        bannerService.removeById(room.getId());
        return PTResult.ok();
    }


    @GetMapping("/getNoticeList")
    public PTResult<Page<List<XxNotice>>> getNoticeList(@RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "10") int limit) {
        int index = (page - 1) * limit;
        List<XxNotice> list = noticeService.lambdaQuery().orderByDesc(XxNotice::getCreateTime).last(String.format(" limit %d,%d",index,limit)).list();
        long total = noticeService.lambdaQuery().count();
        Page result = new Page(page,limit,total);
        result.setRecords(list);
        return PTResult.ok(result);
    }

    @PostMapping("/addOrEditNotice")
    public PTResult addOrEditBanner(@RequestBody XxNotice item) {
        if (item.getId() == null){
            item.setCreateTime(new Date());
            noticeService.save(item);
        }else {
            noticeService.lambdaUpdate().eq(XxNotice::getId,item.getId()).set(XxNotice::getTitle,item.getTitle()).set(XxNotice::getContent,item.getContent()).set(XxNotice::getPic,item.getPic()).update();
        }
        return PTResult.ok();
    }

    @PostMapping("/deleteNotice")
    public PTResult deleteBanner(@RequestBody XxNotice item) {
        noticeService.removeById(item.getId());
        return PTResult.ok();
    }


}
